import { computed, Ref } from "vue";
import { getquotationPageListApi, getWarehouseShipmentQuotationExcelListApi, quotationPageStatApi } from "@/api/waybillManagement/warehouseShipment";
import CommonHook from "@/hook/commonHook";
import { message } from "ant-design-vue";

export default function OperationHook(state: {
  activeKey: Ref<string>;
  searchForm: WarehouseWaybill.SearchForm;
  loading: Ref<boolean>;
  tableData: Ref<WarehouseWaybill.ListRows[]>;
  pageStat: Ref<Base.GeneraDataType>;
  drawerOpts: Base.DrawerOpts;
  tableRef: Ref<any>;
}) {
  const { activeKey, searchForm, loading, tableData, pageStat, drawerOpts, tableRef } = state;
  const selectRows = computed<Array<WarehouseWaybill.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { getNoZeroKey } = CommonHook();

  const getData = async () => {
    const params = {
      ...getParams()
    };

    loading.value = true;
    return getquotationPageListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = {
      ...getParams()
    };
    return quotationPageStatApi(params).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          "1": data[1],
          "2": data[2],
          "3": data[3],
          "4": data[4]
        });
        activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2", "3", "4"]));
      }
    });
  };
  const changeTabHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  const getParams = () => {
    const { bizIdList, warehouseCodeList, customerIdList, followIdList, pageNum, pageSize, companyIdList, addressType, channelIdList, warehouseBillNumList } = searchForm;
    const params: WarehouseWaybill.SearchFormParams = {
      bizIdList,
      warehouseCodeList,
      customerIdList,
      followIdList,
      warehouseBillNumList: [],
      pageNum,
      pageSize,
      companyIdList,
      addressType,
      channelIdList,
      state: Number(activeKey.value)
    };
    if (warehouseBillNumList)
      params.warehouseBillNumList = warehouseBillNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");

    if (channelIdList) {
      params.channelIdList = channelIdList.map((t: any) => t[t.length - 1]);
    }

    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };

  const offerHandle = (row: WaybillQuotation.ListRows) => {
    drawerOpts.title = `报价`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "Offer";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  //批量导出
  const exportHandle = () => {
    const params = {
      ...getParams(),
      warehouseBillNumList: selectRows.value.length ? selectRows.value.map((t: any) => t.warehouseBillNum) : undefined,
      status: activeKey.value
    };
    return getWarehouseShipmentQuotationExcelListApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!请前往导出中心导出");
        getData();
      }
    });
  };

  return {
    getData,
    searchHandle,
    changeTabHandle,
    offerHandle,
    exportHandle
  };
}
